#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
批量分析所有OpenBMC库的ABI语义
"""

import sys
from pathlib import Path

# 添加当前目录到path
sys.path.insert(0, str(Path(__file__).parent))

from abi_analyzer import ABIAnalyzer


def main():
    """批量分析所有库"""
    project_root = Path(__file__).parent.parent.parent
    abi_dir = project_root / 'output' / 'abi_data'
    analyzed_dir = project_root / 'output' / 'analyzed'
    
    # 创建输出目录
    analyzed_dir.mkdir(parents=True, exist_ok=True)
    
    # 获取所有JSON文件
    json_files = sorted(abi_dir.glob('*.abi.json'))
    
    print(f"[INFO] 找到 {len(json_files)} 个库需要分析")
    print("=" * 80)
    
    success = 0
    failed = 0
    
    for idx, json_file in enumerate(json_files, 1):
        lib_name = json_file.stem.replace('.abi', '')
        output_file = analyzed_dir / f"{lib_name}.analyzed.xlsx"
        
        print(f"\n[{idx}/{len(json_files)}] 分析 {lib_name} ...")
        
        try:
            analyzer = ABIAnalyzer(str(json_file))
            analyzer.analyze()
            analyzer.save_to_excel(str(output_file))
            analyzer.print_summary()
            success += 1
        except Exception as e:
            print(f"  ✗ 失败: {e}")
            failed += 1
    
    # 总结
    print("\n" + "=" * 80)
    print("批量分析完成！")
    print(f"成功: {success}/{len(json_files)}")
    if failed > 0:
        print(f"失败: {failed}")
    print(f"输出目录: {analyzed_dir}")
    print("=" * 80)
    
    return 0 if failed == 0 else 1


if __name__ == '__main__':
    sys.exit(main())

